package com.jsoft.service.impl;/**
 * @author snkstart
 * @create 2023-02-23 0:00
 */

import com.jsoft.dao.TicketEnterInstiDao;
import com.jsoft.dao.impl.TicketEnterInstiDaoImpl;
import com.jsoft.entity.TicketOpen;
import com.jsoft.entity.po.TicketEnterInsti;
import com.jsoft.service.TicketEnterInstiService;
import com.mysql.jdbc.StringUtils;


import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @ClassName TicketEnterInsiServiceImpl
 * @Description TODO
 * @Author jindong
 * @Date 2023/2/23 0:00
 * @Version 1.0
 */

public class TicketEnterInsiServiceImpl implements TicketEnterInstiService {

    private TicketEnterInstiDao ticketEnterInstiDao = new TicketEnterInstiDaoImpl();

    @Override
    public List<TicketEnterInsti> selectAllTicketEnterInsti() {
        List<TicketEnterInsti> ticketEnterInstis = null;
        try {
            ticketEnterInstis = ticketEnterInstiDao.selectAll();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ticketEnterInstis;
    }

    @Override
    public TicketEnterInsti findTicketEnterInstiById(Integer tid) {
        TicketEnterInsti ticketEnterInsti = null;
        try {
            ticketEnterInsti = ticketEnterInstiDao.selectTicketEnterInstiById(tid);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ticketEnterInsti;
    }


    /**
     * 根据查询的id改变该条数据的状态为通过
     *
     * @param tid
     * @return 改变的条数
     * @throws Exception
     */
    @Override
    public int updateStatusById(Integer tid) {
        int res = 0;
        try {
            res = ticketEnterInstiDao.updateStatusById(tid);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res;

    }

    /**
     * 根据查询的id改变该条数据的状态为不通过
     *
     * @param tid
     * @return 改变的条数
     * @throws Exception
     */
    @Override
    public int changeStatusById(Integer tid) {
        int res = 0;
        try {
            res = ticketEnterInstiDao.changeStatusById(tid);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res;
    }

    /**
     * 根据传入对象的属性是否为空进行查询操作
     *
     * @param ticketEnterInsti
     * @return
     * @throws Exception
     */
    @Override
    public List<TicketEnterInsti> CheckTicketByView(TicketEnterInsti ticketEnterInsti) throws Exception {
        String maxamount = null;
        String minamount = null;
        if (!StringUtils.isNullOrEmpty(ticketEnterInsti.getMaxamount())) {
            maxamount = String.valueOf(Integer.parseInt(ticketEnterInsti.getMaxamount()) * 10000);
        }
        if (!StringUtils.isNullOrEmpty(ticketEnterInsti.getMinamount())) {
            minamount = String.valueOf(Integer.parseInt(ticketEnterInsti.getMinamount()) * 10000);
        }

        //将数据类型进行转换
        Date createTime = ticketEnterInsti.getCreateTime();
//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

        String no = ticketEnterInsti.getNo();
        String acquirerName = ticketEnterInsti.getAcquirerName();
        String enterpriseName = ticketEnterInsti.getEnterpriseName();
        List<TicketEnterInsti> ticketEnterInstiList = new ArrayList<>();
        List<TicketEnterInsti> queryViewList = ticketEnterInstiDao.CheckTicketByView(no, acquirerName, minamount, maxamount, createTime, enterpriseName);
        ticketEnterInstiList.addAll(queryViewList);
        return ticketEnterInstiList;
    }

    /**
     * 查询付款列表
     * @return
     */
    @Override
    public List<TicketEnterInsti> findOpenList() {
        try {
            return ticketEnterInstiDao.findOpenList();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 根据no查询凭证详情
     *
     * @param no 凭证编号
     * @return 返回凭证详情
     * @throws Exception 将异常抛到controller层
     */
    @Override
    public TicketEnterInsti findDetailByNode(String no) {

        TicketEnterInsti detailByNode = null;
        try {
            detailByNode = ticketEnterInstiDao.findDetailByNode(no);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return detailByNode;
    }

    @Override
    public int updateStatusByNo(String no){
        int res = 0;
        try {
            res = ticketEnterInstiDao.updateStatusByNo(no);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res;
    }

    @Override
    public List<TicketEnterInsti> selectViewByCondition(TicketEnterInsti ticketEnterInsti) {
        String maxamount = null;
        String minamount = null;
        if (!StringUtils.isNullOrEmpty(ticketEnterInsti.getMaxamount())) {
            maxamount = String.valueOf(Integer.parseInt(ticketEnterInsti.getMaxamount()) * 10000);
        }
        if (!StringUtils.isNullOrEmpty(ticketEnterInsti.getMinamount())) {
            minamount = String.valueOf(Integer.parseInt(ticketEnterInsti.getMinamount()) * 10000);
        }

        //将数据类型进行转换
        Date createTime = ticketEnterInsti.getCreateTime();
//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

        String no = ticketEnterInsti.getNo();
        String acquirerName = ticketEnterInsti.getAcquirerName();
        String enterpriseName = ticketEnterInsti.getEnterpriseName();
        List<TicketEnterInsti> ticketEnterInstiList = new ArrayList<>();
        List<TicketEnterInsti> queryViewList = null;
        try {
            queryViewList = ticketEnterInstiDao.selectViewByCondition(no, acquirerName, minamount, maxamount, createTime, enterpriseName);
        } catch (Exception e) {

        }
        ticketEnterInstiList.addAll(queryViewList);
        return ticketEnterInstiList;
    }


}
